package com.yh.service.impl;

import com.yh.service.MyWebApplicationService;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Set;

/**
 * @author yuanhao
 * @version 1.0
 * @description
 * @email yuanhao@yinhai.com
 * @date 2022/4/14 11:14
 */
@HandlesTypes(MyWebApplicationService.class)
public class MySpringServletContainerInitializer implements ServletContainerInitializer {
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        System.out.println("模拟spring初始化");
        if (set!=null){
            Iterator<Class<?>> iterator = set.iterator();
            while (iterator.hasNext()){
                Class<?> next = iterator.next();
                if (!next.isInterface() && !Modifier.isAbstract(next.getModifiers()) &&MyWebApplicationService.class.isAssignableFrom(next)){
                    try {
                        ((MyWebApplicationService)next.newInstance()).onStartup(servletContext);
                    } catch (Throwable var7) {
                        throw new ServletException("Failed to instantiate WebApplicationInitializer class", var7);
                    }
                }
            }
        }
    }
}
